package primalgorithms.list;

import java.util.HashMap;

public class _06HasCycle {
    public static boolean hasCycle(ListNode head) {
        //判断是否是存在环序列
        //等价于判断是否有某个结点的next指向前面已经重复的结点地址
        //哈希表法 ==> 查重
        //效率一般
        if(head == null || head.next == null){
            return false;
        }
        HashMap<ListNode,Integer> record = new HashMap<>();
        ListNode temp = head;
        while (temp != null){
            if(!record.containsKey(temp)){
                record.put(temp,record.getOrDefault(temp,0)+1);
            }else {
                //说明哈希表中已经存在该节点了
                return true;
            }
            temp = temp.next;
        }
        return false;
    }
}
